//
//  DayAccountViewController.m
//  BusinessApp
//
//  Created by macox on 16/10/6.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "DayAccountViewController.h"
#import "DayAccountTableViewCell.h"
#import "DayAccountDetailViewController.h"
#import "LuShuModel.h"
@interface DayAccountViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *DayAccounttableView;
@property (nonatomic , strong) NSMutableArray *ChanelDataArray;
@property (nonatomic , strong) NSMutableArray *goodDataListRefresh;
@property (nonatomic , assign) NSInteger pageNumber;//
@end

@implementation DayAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNumber = 1;
    [self requestData];
    self.navigationItem.title = @"流水账";
    UIImage *classBackImage = [UIImage imageNamed:@"zuojiantou@3x-1"];
    classBackImage = [classBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:classBackImage style:UIBarButtonItemStylePlain target:self action:@selector(classBackAction)];
    [_DayAccounttableView registerNib:[UINib nibWithNibName:@"DayAccountTableViewCell" bundle:nil] forCellReuseIdentifier:@"DayAccountTableViewCell"];
    
    _DayAccounttableView.delegate = self;
    _DayAccounttableView.dataSource = self;
    _DayAccounttableView.tableFooterView = [[UIView alloc] init];
    // Do any additional setup after loading the view from its nib.
}
#pragma mark -- 请求数据
- (void)requestData{
    _ChanelDataArray = [NSMutableArray array];
    NSDictionary *param = @{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"Index":@"1",@"MaxId":@"0"};
    [GDJHttpTool get:GetJiLuPagerBefor params:param success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        NSLog(@"%@", param);
        if ([[responseObj objectForKey:@"Err"] intValue]) {
            
        }else{
            
            _ChanelDataArray =   [LuShuModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"JiLuInfos"]];
            NSLog(@"+++++++++++%lu", (unsigned long)_ChanelDataArray.count)  ;
            
        }
        if(_ChanelDataArray.count !=0){
            LuShuModel * model = _ChanelDataArray[0];
            //
            [_DayAccounttableView reloadData];
            [self example21:[NSString stringWithFormat:@"%d",model.Id]];
            
        }
      
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}
#pragma mark -- 刷新加载数据
- (void)example21:(NSString *)MaID{
    __unsafe_unretained __typeof(self) weakSelf = self;
    NSLog(@"查询");
    //        weakSelf.storeListTableView.mj_header.hidden = NO;
    // 下拉刷新
    weakSelf->_DayAccounttableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        //        pageNumber = 1;
        [self requestData];
        
        
        [weakSelf->_DayAccounttableView reloadData];
        // 结束刷新
        [weakSelf->_DayAccounttableView.mj_header endRefreshing];
        
    }];
    //    [weakSelf.sellCenterTableV.mj_header beginRefreshing];
    // 上拉刷新
    weakSelf->_DayAccounttableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        _pageNumber ++;
        [self requestDataListRefreshMore:[NSString stringWithFormat:@"%ld", (long)_pageNumber ]:MaID];
        
        [weakSelf->_DayAccounttableView reloadData];
        // 结束刷新
        [weakSelf->_DayAccounttableView.mj_footer endRefreshing];
        
    }];
    // 默认先隐藏footer
    weakSelf->_DayAccounttableView.mj_footer.hidden = NO;
}
#pragma mark -- 请求下一页的数据
- (void)requestDataListRefreshMore:(NSString *)pageNumber1:(NSString *)MaxId{
    
    //    ChanelDataArray = [NSMutableArray array];
    _goodDataListRefresh = [NSMutableArray array];
    NSDictionary *param = @{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"Index":pageNumber1,@"MaxId":MaxId};
    [GDJHttpTool get:GetJiLuPagerBefor params:param success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        NSLog(@"%@", param);
        if ([[responseObj objectForKey:@"Err"] intValue]) {
            
        }else{
            
            _goodDataListRefresh =   [LuShuModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"JiLuInfos"]];
            [_ChanelDataArray addObjectsFromArray:_goodDataListRefresh];
            NSLog(@"+++++++%lu",(unsigned long)_ChanelDataArray.count);
            
            
        }
        
        [_DayAccounttableView reloadData];
        //        [self example21:[NSString stringWithFormat:@"%d",model.Id]];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

#pragma mark -- 返回
- (void)classBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - TableView的dataSource协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _ChanelDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DayAccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DayAccountTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    LuShuModel *model = _ChanelDataArray [indexPath.row];
    cell.model = model;
    //    [cell setModel:[self.goodDataArr objectAtIndex:indexPath.row]];
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    DayAccountDetailViewController *dayAccountDeatilVC = [[DayAccountDetailViewController alloc]init];
//    [self.navigationController pushViewController:dayAccountDeatilVC animated:YES];
//    NSLog(@"%ld",(long)indexPath.row);
//}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 84;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
